package com.bilibili.bilibili.network;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.bilibili.bilibili.R;

/**
 * Created by Sir 冯  lenovo on 2017/3/17.
 */

public class JudgeNetwork {

    /**
     * Unknown network class
     */
    private static final int NETWORK_CLASS_UNKNOWN = 0;

    /**
     * wifi net work
     */
    private static final int NETWORK_WIFI = 1;

    /**
     * "2G" networks
     */
    private static final int NETWORK_CLASS_2_G = 2;

    /**
     * "3G" networks
     */
    private static final int NETWORK_CLASS_3_G = 3;

    /**
     * "4G" networks
     */
    private static final int NETWORK_CLASS_4_G = 4;
    private volatile static JudgeNetwork singleton;
    private JudgeNetwork (){}
    public static JudgeNetwork getSingleton() {
        if (singleton == null) {
            synchronized (JudgeNetwork.class) {
                if (singleton == null) {
                    singleton = new JudgeNetwork();
                }
            }
        }
        return singleton;
    }
    /*
 * 判断网络连接是否已开
 * true 已打开  false 未打开
 * */
    private static boolean isConn(Context context){
        boolean bisConnFlag=false;
        ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = conManager.getActiveNetworkInfo();
        if(network!=null){
            bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
        }
        return bisConnFlag;
    }

    /**
     * 当判断当前手机没有网络时选择是否打开网络设置
     * @param context
     */
    private static void showNoNetWorkDlg(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.ic_launcher)         //
                .setTitle(R.string.app_name)            //
                .setMessage("当前无网络").setPositiveButton("设置", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 跳转到系统的网络设置界面
                Intent intent = null;
                // 先判断当前系统版本
                if(android.os.Build.VERSION.SDK_INT > 10){  // 3.0以上
                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                }else{
                    intent = new Intent();
                    intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
                }
                context.startActivity(intent);

            }
        }).setNegativeButton("知道了", null).setCancelable(false).show();
    }


    //判断手机连接的是WIFI还是数据流量
    private static int getNetWorkStatus(Context context) {
        int netWorkType = NETWORK_CLASS_UNKNOWN;

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            int type = networkInfo.getType();

            if (type == ConnectivityManager.TYPE_WIFI) {
                netWorkType = NETWORK_WIFI;
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                netWorkType = getNetWorkClass(context);
            }
        }

        return netWorkType;
    }




    //判断是2G/3G/4G网络
    private static int getNetWorkClass(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;

            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;

            case TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;

            default:
                return NETWORK_CLASS_UNKNOWN;
        }
    }
    public void getJudge(Context context){
        int netWorkClass = getNetWorkClass(context);

        int netWorkStatus = getNetWorkStatus(context);
        switch (netWorkStatus){
            case 0:showNoNetWorkDlg(context);
                break;
            case 1:
                Toast.makeText(context,"当前属于WIFI网",Toast.LENGTH_LONG).show();
                break;
            case 2:Toast.makeText(context,netWorkClass+"G网",Toast.LENGTH_LONG).show();
                break;
            case 3:Toast.makeText(context,netWorkClass+"G网",Toast.LENGTH_LONG).show();
                break;
            case 4:Toast.makeText(context,netWorkClass+"G网",Toast.LENGTH_LONG).show();
                break;
        }
    }

}
